<?php

class newsletter extends a_admin_controller {

    protected $srv;
    protected $letter;
    protected $op;
    public function __construct() {
        parent::__construct(FALSE);
        $this->run_auto_view = FALSE;
        $this->srv = new service_l_newsl8r();
        $this->letter = new letter_m_newsl8r();
    }

    public function create() {
        $this->op = "new";
        if ($this->is_post()) {
            $this->letter->group_id = "1"; //@TO_DO : in future we will have groups for letter
            $this->letter->name = $this->input->post("name");
            $this->letter->content = $this->input->post("email_content");
            $this->letter->description = $this->input->post("desc");

            if ($this->letter->name && $this->letter->content && $this->letter->group_id) {
                $obj= $this->srv->crealte_a_letter($this->letter->name, $this->letter->group_id, $this->letter->description, $this->letter->content);
                if ($obj) {
                    $this->letter = $obj;
                    $this->add_global_msg("News Letter is saved!");
                } else {
                    $this->add_global_msg("Could not save!");
                }
            } else {
                $this->add_global_msg("Required field(s) are missing!");
            }
        }
        
        $this->view("create_letter");
    }

    public function update($id="") {
        $this->op = "update";
        if ($this->is_post()) {
            $this->letter->group_id = "1"; //@TO_DO : in future we will have groups for letter
            $this->letter->name = $this->input->post("name");
            $this->letter->content = $this->input->post("email_content");
            $this->letter->description = $this->input->post("desc");
            $lid = $this->input->post("id");

            if ($this->letter->name && $this->letter->content && $this->letter->group_id && $lid) {
                $obj = $this->srv->update_a_letter($lid, $this->letter->name, $this->letter->group_id , $this->letter->description, $this->letter->content);
                if ($obj) {
                    $this->add_global_msg("News Letter is updated!");
                    $this->letter = $obj;
                } else {
                    $this->add_global_msg("Could not update!");
                }
            }else{
                $this->add_global_msg("Required field(s) are missing!");
            }
        }else{
            $obj = $this->srv->get_letter_obj($id);
            if($obj==NULL){
                show_error("Could not find the newsltter with id $id");
            }else{
                $this->letter = $obj;
            }
        }
        
        $this->view("create_letter");
    }

    public function manage() {
        $group_id = "1"; //@TO_DO : groups would be added in future
        $letters = $this->srv->get_all_letters($group_id);
        if ($this->input->post("letter_id")) {
            $obj = $this->srv->get_letter_obj($id);
            $this->assign("letter", $obj);
        }
        $this->assign("letters", $letters);
        $this->view("manage");
    }

    public function delete_letter(){
        $id = $this->input->post("letter_id");
        if(!empty ($id ) && is_numeric($id) ){
            return $this->srv->delete_letter($id)? "ok":"nok";
        }else{
            return "nok";
        }
    }
    
    public function batch_assign() {
        $this->assign("letter", $this->letter);
        $this->assign("op", $this->op);
    }

    public function __destruct() {
        unset($this->srv);
    }

}

?>